import { Messages } from '@Config/config'
import { message } from 'ant-design-vue'
import { getUserToken } from '../localStorage'
import { URL_PRIX } from '@Config/config'

//请求母体
export default async (url, params) => {
  // let router = useRouter()
  let TOKEN = getUserToken()
  try {
    let pro = await fetch(URL_PRIX + url, {
      method: 'POST',
      // mode: 'cors',

      headers: {
        usertokenxm: TOKEN,
        'Content-Type': 'application/json',
        //  Referer: URL_PRIX + url,
      },
      //     credentials: 'include',
      ...params,
    }).catch((e) => {
      Messages.add(message.error('发生错误' + e))
    })

    //console.log(pro.status)
    if (pro.status != 200) {
      // let match = window.location.href.match(/.*#/gi)
      // let href = match.length > 0 ? match[0] : null
      // href && (location.href = href)
      // message.error('发生错误' + _data.msg)
      throw pro.status

      // return Messages.add(message.error('发生错误' + pro.status))
    }

    let _data = await pro.json()

    if (_data.code == 2) {
      let match = window.location.href.match(/.*#/gi)
      let href = match.length > 0 ? match[0] : null
      href && (location.href = href)
      message.error('发生错误' + _data.msg)
      //router.push({ path: '/' })
      // return
    }
    return _data
  } catch (e) {
    Messages.add(message.error('发生错误' + e))

    return { code: 99999, msg: e.toString(), data: null }
  }
}
